package com.huawei.hwid.api.common;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import com.huawei.cloudservice.CloudAccount;
import com.huawei.cloudservice.CloudAccountManager;
import com.huawei.cloudservice.CloudRequestHandler;
import com.huawei.cloudservice.LoginHandler;
import com.huawei.hwid.core.b.a.a;
import com.huawei.hwid.core.b.a.a.e;
import com.huawei.hwid.core.constants.HwAccountConstants;
import com.huawei.hwid.core.d.b;
import com.huawei.hwid.core.d.l;
import com.huawei.hwid.core.datatype.HwAccount;
import com.huawei.hwid.core.helper.handler.ErrorStatus;

class p implements CloudRequestHandler {
    Activity a;
    CloudRequestHandler b;
    HwAccount c;

    p(Activity activity, CloudRequestHandler cloudRequestHandler, HwAccount hwAccount) {
        this.a = activity;
        this.b = cloudRequestHandler;
        this.c = hwAccount;
    }

    private static void a(final Context context, final String str, final String str2, final String str3) {
        new Thread(new Runnable() {
            public void run() {
                a eVar = new e(context, str, "com.huawei.hwid", str3);
                eVar.a(context, eVar, str2, new com.huawei.hwid.core.helper.handler.a(this, context) {
                    final /* synthetic */ AnonymousClass1 a;

                    public void a(Bundle bundle) {
                        com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "onSuccess", true);
                    }

                    public void b(Bundle bundle) {
                        com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "onFail", true);
                    }
                });
            }
        }).start();
    }

    public void onFinish(Bundle bundle) {
        if (bundle != null && 1 == bundle.getInt(CloudAccountManager.KEY_RESULT_CODE)) {
            Bundle bundle2 = new Bundle();
            bundle2.putString("accountName", this.c.b());
            bundle2.putString(HwAccountConstants.PARA_ACCOUNT_SERVICETOKEN, this.c.g());
            a(this.a, this.c.d(), this.b, bundle2);
        }
    }

    public void onError(ErrorStatus errorStatus) {
    }

    private void a(Context context, String str, CloudRequestHandler cloudRequestHandler, Bundle bundle) {
        if (!b(context, str, cloudRequestHandler, bundle)) {
            if (o.b(context)) {
                String g;
                com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "getHwAccount", true);
                HwAccount hwAccount = null;
                CloudAccount b = e.b(context, str);
                if (b != null) {
                    hwAccount = b.getAccountData();
                }
                String str2 = "";
                String str3 = "";
                if (hwAccount != null) {
                    str3 = hwAccount.b();
                    g = hwAccount.g();
                } else {
                    g = str3;
                    str3 = str2;
                }
                if (TextUtils.isEmpty(str3)) {
                    str3 = bundle.getString("accountName");
                }
                if (TextUtils.isEmpty(g)) {
                    g = bundle.getString(HwAccountConstants.PARA_ACCOUNT_SERVICETOKEN);
                }
                if (TextUtils.isEmpty(str3) || TextUtils.isEmpty(g)) {
                    if (cloudRequestHandler != null) {
                        cloudRequestHandler.onError(new ErrorStatus(12, "accountName or serviceToken is empty"));
                        com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "accountName or serviceToken is empty", true);
                    }
                } else if (b.a(context)) {
                    a(this.a);
                    e.b(context);
                    Bundle bundle2 = new Bundle();
                    bundle2.putInt(CloudAccountManager.KEY_RESULT_CODE, 1);
                    cloudRequestHandler.onFinish(bundle2);
                    if (!e.a(context, str3)) {
                        a(context, str, str3, g);
                    }
                } else if (cloudRequestHandler != null) {
                    cloudRequestHandler.onError(new ErrorStatus(5, context.getString(l.a(context, "CS_no_network_content"))));
                    com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "network is not avaible", true);
                }
            } else if (cloudRequestHandler != null) {
                Bundle bundle3 = new Bundle();
                bundle3.putInt(CloudAccountManager.KEY_RESULT_CODE, 1);
                cloudRequestHandler.onFinish(bundle3);
            }
        }
    }

    private boolean b(Context context, String str, CloudRequestHandler cloudRequestHandler, Bundle bundle) {
        com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "logoutHwIdByUserId", true);
        if (!o.a(context, cloudRequestHandler)) {
            com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "logoutHwIdByUserId: context or cloudRequestHandler is null", true);
            return true;
        } else if (!TextUtils.isEmpty(str)) {
            return false;
        } else {
            if (cloudRequestHandler != null) {
                cloudRequestHandler.onError(new ErrorStatus(12, "userId is empty"));
            }
            com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "userId is empty", true);
            return true;
        }
    }

    public void a(Context context) {
        if (context == null) {
            com.huawei.hwid.core.d.b.e.b("TmpCloudRequestHandler", "context is null", true);
            return;
        }
        if (b.h(context) && o.b(context)) {
            com.huawei.hwid.api.common.apkimpl.a.a(context, this.c);
        }
        o.a(context, "");
        com.huawei.hwid.c.a.a(context).b(context, b.l(context));
        LoginHandler a = o.a();
        if (a != null) {
            a.onLogout(new CloudAccount[0], -1);
        }
    }
}
